<?php

namespace App\Admin\Controllers\Users;

use App\Admin\Repositories\Users\TalentApply;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TalentApplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TalentApply(['user']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.name','用户名');
//            $grid->column('tag');
            $grid->column('talent_id')->using([1=>'心理咨询师',2=>'占卜师',3=>'倾听师']);
            $grid->column('name');
            $grid->column('mobile');
            $grid->column('age_limit');
            $grid->column('certificate')->image('',100,100);;
            $grid->column('status')->using([0=>'待审核',1=>'审核通过',2=>'未通过'])->filter();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TalentApply(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('tag');
            $show->field('talent_id');
            $show->field('name');
            $show->field('mobile');
            $show->field('age_limit');
            $show->field('certificate');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TalentApply(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('tag');
            $form->display('talent_id');
            $form->text('name');
            $form->display('mobile');
            $form->text('age_limit');
            $form->image('certificate');
            $form->select('status')->options([0=>'待审核',1=>'审核通过',2=>'未通过'])->saving(function ($v) use ($form) {
                if($v==1) {
                    $item = $form->model();
                    (new TalentApply())->apply($item->user_id, $item->talent_id,$item->age_limit);
                }
                return $v;
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
